package graphicInterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.html.HTMLDocument.Iterator;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import category.*;



public class GUI_AddCategory extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    JList sovraList;
    JList subList;
    JList avList;
    JList childList;
    JComboBox comboBoxFather;
    JButton addChild;
    JButton remChild;
    JButton Ok;
    JButton Canc;
    
    Vector<String> allCat;
    Vector<String> sovraCat;
    Vector<String> subCat;
    Vector<String> availableCat;
    Vector<String> childs;
    

    /**
     * Create the frame.
     */
    public GUI_AddCategory(final CategoryCollection Coll) {
            
            allCat=new Vector<String>();
            sovraCat=new Vector<String>();
            subCat=new Vector<String>();
            availableCat=new Vector<String>();
            childs=new Vector<String>();
            
            setTitle("Add Category");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 850, 600);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(5, 5));
            setContentPane(contentPane);
            
            JLabel name = new JLabel("Insert the name of the category:");
            JLabel father = new JLabel("Select a parent category:");
            JLabel warning = new JLabel("WARNING: changing parent category will clear childs list");
            warning.setForeground(Color.red);
            
            textField = new JTextField();
            textField.setColumns(30);
            
            allCat = getMainVector(Coll);
            comboBoxFather = new JComboBox(allCat);
            comboBoxFather.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent event) {
                    if (event.getStateChange() == ItemEvent.SELECTED) {
                       String item = (String)event.getItem();
                       sovraCat=getParentVector(Coll, item);
                       availableCat=getAvailableVector(Coll, sovraCat);
                       sovraList.setListData(sovraCat);
                       avList.setListData(availableCat);
                       subCat=new Vector<String>();
                       childs=new Vector<String>();
                       subList.setListData(subCat);
                       childList.setListData(childs);
                    }
                 }
            });
            
            JPanel nameBar = new JPanel(new BorderLayout(5,5));
            nameBar.add(name, BorderLayout.WEST);
            nameBar.add(textField, BorderLayout.CENTER);
            JPanel fatherBar = new JPanel(new BorderLayout(5,5));
            fatherBar.add(father, BorderLayout.WEST);
            fatherBar.add(comboBoxFather, BorderLayout.CENTER);
            fatherBar.add(warning, BorderLayout.EAST);
            JPanel topPanel=new JPanel(new GridLayout(2,1,10,10));
            topPanel.add(nameBar);
            topPanel.add(fatherBar);
            topPanel.setBorder(new TitledBorder("Category Info"));
            
            contentPane.add(topPanel, BorderLayout.NORTH);
            
            JPanel leftPanel=new JPanel(new GridLayout(1,2,15,5));
            JPanel rightPanel=new JPanel(new GridLayout(1,2,15,5));
            
            JPanel llPanel=new JPanel(new BorderLayout());
            JPanel lrPanel=new JPanel(new BorderLayout());
            JPanel rlPanel=new JPanel(new BorderLayout());
            JPanel rrPanel=new JPanel(new BorderLayout());
            
            JLabel lab1=new JLabel("Parent Categories");
            JLabel lab2=new JLabel("Available Categories");
            JLabel lab3=new JLabel("Child Categories");
            JLabel lab4=new JLabel("Derived Categories");
            
                   
            sovraList = new JList();
            sovraList.setEnabled(false);
            sovraList.setPreferredSize(new Dimension(160,100));
            subList = new JList();
            subList.setPreferredSize(new Dimension(160,100));
            subList.setEnabled(false);
            availableCat=getAvailableVector(Coll, new Vector<String>());
            avList = new JList(availableCat);
            //listTot.setSize(80, 80);
            childList = new JList();
            //listChild.setSize(80, 80);
            
            
            llPanel.add(lab1,BorderLayout.NORTH);
            llPanel.add(sovraList,BorderLayout.CENTER);
            lrPanel.add(lab2,BorderLayout.NORTH);
            lrPanel.add(avList,BorderLayout.CENTER);
            rlPanel.add(lab3,BorderLayout.NORTH);
            rlPanel.add(childList,BorderLayout.CENTER);
            rrPanel.add(lab4,BorderLayout.NORTH);
            rrPanel.add(subList,BorderLayout.CENTER);
            
            leftPanel.add(llPanel);
            leftPanel.add(lrPanel);
            rightPanel.add(rlPanel);
            rightPanel.add(rrPanel);
            
            addChild = new JButton(">>");
            remChild = new JButton("<<");
            
            addChild.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                      String sel=(String)avList.getSelectedValue();
                      availableCat.remove(sel);
                      childs.add(sel);
                      subCat=getSubCatVector(Coll, childs);
                      avList.setListData(availableCat);
                      childList.setListData(childs);
                      subList.setListData(subCat);
                  }
                });
            
            remChild.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e)
              {
                  String sel=(String)childList.getSelectedValue();
                  childs.remove(sel);
                  availableCat.add(sel);
                  subCat=getSubCatVector(Coll, childs);
                  avList.setListData(availableCat);
                  childList.setListData(childs);
                  subList.setListData(subCat);
              }
            });
                    
            
            JPanel mainPanel = new JPanel(new BorderLayout(30,20));
            JLabel info = new JLabel("<html>Here you can add subcategories to your category.<br> Select a category from the available list and move it to the childs list by clicking on the '>>' button.<br> On the left you can see all the partens categories, while on the right you'll see all the categories that derives from this one</html>");
            info.setMinimumSize(new Dimension(500,200));
                   
            JPanel centralPanel = new JPanel(new GridLayout(4,1,20,20));
            centralPanel.add(new JPanel());
            centralPanel.add(addChild);
            centralPanel.add(remChild);
            centralPanel.add(new JPanel());
            
            mainPanel.add(info, BorderLayout.NORTH);
            mainPanel.add(centralPanel, BorderLayout.CENTER);
            
            mainPanel.add(leftPanel, BorderLayout.WEST);
            mainPanel.add(rightPanel, BorderLayout.EAST);
            
            mainPanel.setBorder(new TitledBorder("Subcategories"));
            
            contentPane.add(mainPanel, BorderLayout.CENTER);
            
            Ok = new JButton("OK");
            Canc = new JButton("Cancel");
            
            Canc.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent e)
                  {
                      dispose();
                  }
                });
            
            Ok.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e)
              {
                  CategoryInfo ci=new CategoryInfo(textField.getText(), comboBoxFather.getSelectedItem().toString(), childs);
                  // CHIAMARE LA FUNZIONE DEL CORE CHE AGGIORNA IL DB
                  // CateogryCore.addCategoryToDb(ci);
                  dispose();
              }
            });
            
            JPanel bottomBar = new JPanel(/*new BoxLayout()*/);
            bottomBar.add(Canc);
            bottomBar.add(Ok);
            
            
            contentPane.add(bottomBar, BorderLayout.SOUTH);
            
        }
    
    public Vector<String> getMainVector(CategoryCollection cat){
        Vector<String> v=new Vector<String>();
        for (String c : cat.getCategoryList().keySet()){
            v.add(c);
        }
        return v;
    }
    
    public Vector<String> getParentVector(CategoryCollection cat, String fatherName){
        Vector<String> v=new Vector<String>();
        v.add("");
        v.add(fatherName);
        Category c=cat.getCategoryList().get(fatherName);
        while (c.getFather()!=null){
            c=c.getFather();
            v.add(c.getName());
        }
        return v;
    }
    
    public Vector<String> getAvailableVector(CategoryCollection cat, Vector<String> parents){
        Vector<String> v=new Vector<String>();
        for (String c : cat.getCategoryList().keySet()){
            if (!parents.contains(c)) v.add(c);
        }
        
        return v;
    }
    
    public Vector<String> getSubCatVector(CategoryCollection cat, Vector<String> childs){
        Vector<String> v=new Vector<String>();
        
        for (String catName : childs){
            Category c=cat.getCategoryList().get(catName);
            if (!v.contains(catName))v.add(catName);
            v=visit(c,v);
        }
        return v;
    }
    
    public Vector<String> visit(Category c, Vector<String> v){
        for (Category subC : c.getSubCategories()){
            if (!v.contains(subC.getName()))v.add(subC.getName());
            v=visit(subC, v);
        }
        return v;
    }

}